package com.program.beautymakeup.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.collection.ListUtil;
import com.program.beautymakeup.common.OperLog;
import com.program.beautymakeup.controller.form.PortFolioMakeupForm;
import com.program.beautymakeup.controller.form.PortfolioForm;
import com.program.beautymakeup.domain.bo.PortfolioQueryBO;
import com.program.beautymakeup.domain.entity.Portfolio;
import com.program.beautymakeup.domain.entity.PortfolioImg;
import com.program.beautymakeup.domain.entity.PortfolioMakeup;
import com.program.beautymakeup.domain.vo.PageVO;
import com.program.beautymakeup.domain.vo.PortfolioRsp;
import com.program.beautymakeup.domain.vo.ResultInfo;
import com.program.beautymakeup.mapper.PortfolioImgMapper;
import com.program.beautymakeup.mapper.PortfolioMakeupMapper;
import com.program.beautymakeup.mapper.PortfolioMapper;
import com.program.beautymakeup.service.PortfolioService;
import com.program.beautymakeup.service.UploadService;
import com.program.beautymakeup.utils.ResultInfoUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author CaiKe
 * @date 2023/2/11 14:46
 */

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/portfolio")
public class PortfoliosController {

    private final UploadService uploadService;
    private final PortfolioMakeupMapper makeupMapper;
    private final PortfolioMapper portfolioMapper;
    private final PortfolioImgMapper imgMapper;
    private final PortfolioService portfolioService;


    @SaCheckLogin
    @PostMapping("/makeup")
    public ResultInfo<Void> addMakeupImg(@Validated PortFolioMakeupForm form) {
        final PortfolioMakeup makeupToSave = PortfolioMakeup.builder()
                .userId(form.getUserId())
                .url(form.getFileUrl())
                .comment(form.getComment()).build();
        makeupMapper.insert(makeupToSave);
        return ResultInfoUtil.buildSuccess();
    }

    @SaCheckLogin
    @DeleteMapping("/makeup/{id}")
    @OperLog(module = "定妆照模块", type = 2, description = "删除指定定妆照")
    public ResultInfo<Void> deleteMakeupImg(@PathVariable("id") Integer id) {
        makeupMapper.deleteByPrimaryKey(id);
        return ResultInfoUtil.buildSuccess();
    }

    @SaCheckLogin
    @PutMapping("/{id}")
    @OperLog(module = "作品集模块", type = 3, description = "修改作品集文字信息")
    public ResultInfo<Void> updatePortfolioContent(@PathVariable("id") Integer id, @Valid PortfolioForm form) {
        portfolioMapper.updateByPrimaryKeySelective(
                Portfolio.builder()
                        .userId(form.getUserId())
                        .name(form.getName())
                        .type(form.getType())
                        .id(id)
                        .build());
        return ResultInfoUtil.buildSuccess();
    }

    @SaCheckLogin
    @PostMapping("/{id}/img")
    public ResultInfo<Void> addImgToPortfolio(@PathVariable("id") Integer portfolioId, String fileUrl, HttpServletRequest request) {
        imgMapper.insert(PortfolioImg.builder()
                .portfolioId(portfolioId)
                .url(fileUrl)
                .createTime(new Date())
                .build());
        return ResultInfoUtil.buildSuccess();
    }

    @SaCheckLogin
    @DeleteMapping("/{id}/img/{imgId}")
    @OperLog(module = "作品集模块", type = 2, description = "删除作品集中的一张图片")
    public ResultInfo<Void> deleteImgFromPortfolio(@PathVariable("id") Integer portfolioId, @PathVariable("imgId") Integer imgId) {
        final PortfolioImg portfolioImg = imgMapper.selectByPrimaryKey(imgId);
        if (!portfolioId.equals(portfolioImg.getPortfolioId())) {
            return ResultInfoUtil.buildError("该作品集下无此图片");
        }
        List<String> imgs = new ArrayList<>();
        imgs.add(portfolioImg.getUrl());
        uploadService.deleteFile(imgs);
        imgMapper.deleteByPrimaryKey(imgId);
        return ResultInfoUtil.buildSuccess();
    }

    @SaCheckLogin
    @DeleteMapping("/{id}")
    @OperLog(module = "作品集模块", type = 2, description = "删除作品集")
    public ResultInfo<Void> deletePortfolio(@PathVariable("id") Integer portfolioId) {
        boolean success = portfolioService.delete(portfolioId);
        return success ? ResultInfoUtil.buildSuccess() : ResultInfoUtil.buildError();
    }

    @PostMapping("/list")
    public ResultInfo<PageVO<PortfolioRsp>> queryList(@Valid @RequestBody PortfolioQueryBO portfolioQueryBO) {
        return ResultInfoUtil.buildSuccess(portfolioService.queryList(portfolioQueryBO));
    }

}